package com.uit.gis.pojo;

import com.uit.gis.util.JsonData;
import com.uit.gis.util.Util.SpatialType;

import processing.data.JSONObject;

public class Rectangle extends Shape {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3533526424889302343L;
	JSONObject rect = new JSONObject();

	int rectX = -1;
	int rectY = -1;
	int rectWidth = -1;
	int rectHeight = -1;
	int clickCount = 0;
	@Override
	public void setup()
	{
		spatialType = SpatialType.RECTANGLE;
		background(255);
	}
	@Override
	public void draw() {
		if(!continueDraw)
			return;
		if (rectX > 0 && rectY > 0 && clickCount != 0) {
			background(255);
			stroke(0);
			//fill(175);
			rectWidth = mouseX - rectX;
			rectHeight = mouseY - rectY;
			rect(rectX, rectY, rectWidth, rectHeight);
		}
	}
	@Override
	public void mousePressed() {
		if(!continueDraw)
			return;
		if (clickCount == 0) {
			rectX = mouseX;
			rectY = mouseY;
			clickCount++;
		}
	}

	@Override
	public void mouseClicked(processing.event.MouseEvent e) {
		if (e.getButton() == processing.core.PConstants.RIGHT) { 
			saveToTextFile();
			continueDraw = false;
		}
		 else {
				rect.setInt("x", rectX);
				rect.setInt("y", rectY);
				rect.setInt("w", rectWidth);
				rect.setInt("h", rectHeight); 
				clickCount = 0;
				rectX = -1;
				rectY = -1;
			}
	}
	@Override
	public void saveToTextFile() {
		// TODO Auto-generated method stub
		saveJSONObject(rect, getSavingPath());
		super.saveToTextFile();
	};
	@Override
	public JsonData loadDataFromFile() {
		// TODO Auto-generated method stub
		JSONObject jsonObject = loadJSONObject(getSavingPath());
		JsonData jsonData = new JsonData(null, jsonObject);
		return jsonData;
	};

}
